<template>
  <div class="CityList" ref="warpper">
    <div>
    <div class="area">
      <div class="title">当前城市</div>
      <div class="button-list">
        <div class="button">{{this.$store.state.city}}</div>
      </div>
    </div>
    <div class="area">
      <div class="title">热门城市</div>
      <div class="button-list">
        <div class="button" 
          v-for="city in hotCities" 
          :key="city.id"
          @click="selectCity(city.name)"
        >
          {{city.name}}
        </div>
      </div>
    </div>
    <div class="area">
      <div v-for="(item, index) in cities" :key="index">
        <div class="title" :ref="index">{{index}}</div>
        <div class="item-list">
          <div class="item" 
            v-for="city in item" 
            :key="city.id"  
            @click="selectCity(city.name)"
          >{{city.name}}</div>
        </div>
      </div>
    </div>
    </div>
  </div>
</template>

<script>
  import BScroll from 'better-scroll'

  export default {
    name: "CityList",
    props: {
      cities: Object,
      hotCities: Array,
      letter: String
    },
    data() {
      return {
        scroll:''
      }
    },
    methods: {
      selectCity(city) {
        this.$store.commit('selectCity', city)
        this.$router.push('/')
      }
    },
    watch: {
      letter() {
        const ele = this.$refs[this.letter][0]
        this.scroll.scrollToElement(ele)
      }
    },
    mounted() {
      this.scroll = new BScroll(this.$refs.warpper, {
        click: true
      })
    }
  }
</script>

<style lang="less" scoped>
  .CityList {
    overflow: hidden;
    position: absolute;
    top: 1.58rem;
    left: 0;
    right: 0;
    bottom: 0;
    .title {
      line-height: .5rem;
      color: #212121;
      font-size: .28rem;
      padding: .2rem;
      background-color: #eee;
    }
    .button-list {
      display: flex;
      flex-wrap: wrap;
      .button {
        width: 33.3%;
        font-size: .32rem;
        line-height: .9rem;
        box-sizing: border-box;
        text-align: center;
        border-right: 1px solid #ccc;
        border-bottom: 1px solid #ccc;
      }
    }
    .item {
      padding-left: .2rem;
      font-size: .32rem;
      line-height: .72rem;
      border-bottom: 1px solid #ccc;
    }
  }
</style>